package com.javasm.gm.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.gm.Gift;
import com.javasm.gm.GiftService;
import com.javasm.gg.GoodsService;
import com.javasm.gm.dao.Gift2GoodsDao;
import com.javasm.gm.Gift2Goods;
import com.javasm.gm.Gift2GoodsService;
import com.javasm.process.ProcessService;
import com.javasm.process.entity.ProcessTask;
import com.javasm.process.entity.TaskStatus;
import com.javasm.sys.CurrentLogin;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Service
public class Gift2GoodsServiceImpl extends ServiceImpl<Gift2GoodsDao, Gift2Goods> implements Gift2GoodsService {
    @Resource
    private ProcessService processService;
    @Resource
    private GiftService giftService;
    @Resource
    private GoodsService goodsService;
    private static String defName="赠品转商品流程";

    @Override
    public Collection<Gift2Goods> listGift2GoodsApply(Gift2Goods obj) {
        return this.baseMapper.listGift2GoodsApply(obj);
    }

    @Transactional
    @Override
    public boolean submitApply(Gift2Goods obj) {
        boolean b=false;
        if(StrUtil.isEmpty(obj.getId()))
            b = save(obj);
        else
            b=updateById(obj);

        b = processService.startProcess(defName, obj.getId());
        return b;
    }

    @Override
    public List<ProcessTask> getTaskByApplyOrderId(String orderId) {
        return processService.getTaskByApplyOrderId(defName,orderId);
    }

    @Override
    public List<ProcessTask> queryToDoTasks() {
        List<ProcessTask> tasks = processService.queryToDoTasks(defName, CurrentLogin.getLoginUserName());
        return tasks;
    }

    @Override
    public List<ProcessTask> queryDosTasks(String o) {
        List<ProcessTask> processTasks = processService.queryDosTasks(defName, CurrentLogin.getLoginUserName(), o);
        return processTasks;
    }

    @Override
    public boolean claim(String orderId) {
        return processService.claim(CurrentLogin.getLoginUserName(),orderId);
    }

    @Transactional
    @Override
    public boolean approve(ProcessTask task) {
        String result = processService.approve(defName, task, CurrentLogin.getLoginUserName());
        if (null == result) return true;

        //流程通过已结束或流程未通过
        Gift2Goods cgcp = new Gift2Goods();
        cgcp.setId(task.getApplyId());
        cgcp.setApplyStatus(result);
        updateById(cgcp);

        if(result.equals(TaskStatus.APPROVE_TG.getStatus())){
            //修改赠品数量,修改商品数量
            Gift2Goods byId = getById(task.getApplyId());
            Integer count = byId.getCount();
            Integer giftId = byId.getGiftId();
            giftService.updateGiftStock(giftId,-count);

            Gift gift = giftService.getById(giftId);
            Integer goodsId = gift.getGoodsId();
            goodsService.updateGoodsStock(goodsId,count);
        }
        return true;
    }

    @Override
    public Gift2Goods getOneById(String id) {
        return baseMapper.getById(id);
    }
}

